package cn.orange.ch07_binarytree;

import cn.orange.TreeNode;

import java.util.LinkedList;

/**
 * LC104.二叉树的最大深度
 */
public class LC104 {
    public int maxDepthD(TreeNode root) { //迭代解法
        if (root == null) {
            return 0;
        }
        LinkedList<TreeNode> que = new LinkedList<>();
        int depth = 0;
        que.offer(root);
        while (!que.isEmpty()) {
            int size = que.size();
            for (int i = 0; i < size; i++) {
                TreeNode poll = que.poll();
                if (poll.left != null) {
                    que.offer(poll.left);
                }
                if (poll.right != null) {
                    que.offer(poll.right);
                }
            }
            depth++;
        }
        return depth;
    }
    public int maxDepth(TreeNode root) { //递归解法
        if (root == null) {
            return 0;
        }
        return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
    }

    public static void main(String[] args) {
        LC104 alg = new LC104();
        TreeNode root = new TreeNode(3);
        root.left = new TreeNode(9);
        root.right = new TreeNode(20);
        root.right.left = new TreeNode(15);
        root.right.right = new TreeNode(7);
        System.out.println(alg.maxDepthD(root));
    }
}
